package main

import (
	"context"
	"errors"
	"fmt"
	"time"
)

func main() {
	ctx, cancelFunc := context.WithCancel(context.Background())
	go wrapJob(ctx)
	time.Sleep(3 * time.Second)
	cancelFunc()

	time.Sleep(time.Hour)
}

func wrapJob(ctx context.Context) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
	}()
	go run()
	for {
		select {
		case <-ctx.Done():
			fmt.Println("手动结束任务")
			panic(errors.New("手动制造异常"))
		default:
		}
	}

}

func run() {
	for i := 0; i < 10; i++ {
		fmt.Println("tick...")
		time.Sleep(1 * time.Second)
	}
}
